package com.itheima.service.impl;



import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SysDictDataMapper;
import com.itheima.dao.SysDictTypeMapper;
import com.itheima.pojo.SysDictData;
import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictDataService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;


@Service
public class SysDictDataServiceImpl implements SysDictDataService {
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;


    /**
     * 添加字典类型
     *
     * @param sysDictData
     */

    @Override
    public void add(SysDictData sysDictData) {
        IdWorker idWorker = new IdWorker();
        long id = idWorker.nextId();
        sysDictData.setUid(String.valueOf(id));
        sysDictData.setCreateByUid("1f01cd1d2f474743b241d74008b12333");
        sysDictData.setUpdateByUid("1f01cd1d2f474743b241d74008b12333");
        sysDictData.setCreateTime(new Date());
        sysDictData.setUpdateTime(new Date());
        sysDictData.setIsPublish("1");
        sysDictDataMapper.insert(sysDictData);
    }

    /**
     * 批量删除字典数据
     *
     * @param sysDictDataList
     */
    @Override
    public void deleteBatch(List<SysDictData> sysDictDataList) {
        if (sysDictDataList.size() > 0 && sysDictDataList != null) {
            for (SysDictData sysDictData : sysDictDataList) {

                sysDictDataMapper.deleteByPrimaryKey(sysDictData.getUid());
            }
        }


    }

    /**
     * 编辑字典类型
     *
     * @param sysDictData
     */
    @Override
    public void edit(SysDictData sysDictData) {
        SysDictData sysDictData1 = sysDictDataMapper.selectByPrimaryKey(sysDictData.getUid());
        sysDictData.setCreateByUid(sysDictData1.getCreateByUid());
        sysDictData.setUpdateByUid(sysDictData1.getUpdateByUid());
        sysDictData.setCreateTime(sysDictData1.getCreateTime());
        sysDictData.setUpdateTime(new Date());

        sysDictDataMapper.deleteByPrimaryKey(sysDictData1.getUid());
        sysDictDataMapper.insert(sysDictData);

    }

    /**
     * 多条件分页查询
     *
     * @param sysDictData
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<SysDictData> getList(SysDictData sysDictData, int page, int size) {
        PageHelper.startPage(page, size);
        String dictLabel = sysDictData.getDictLabel();
        if (dictLabel.equals("") && dictLabel.length()==0){
            sysDictData.setDictLabel(null);
        }
        Example example = createExample(sysDictData);
        List<SysDictData> dataList = sysDictDataMapper.selectByExample(example);
        for (SysDictData dictData : dataList) {
            SysDictType DictType = sysDictTypeMapper.selectByPrimaryKey(dictData.getDictTypeUid());
            String string = JSON.toJSONString(DictType);
            Map sysDictType = (Map)JSON.parse(string);
            dictData.setSysDictType(sysDictType);
        }
        return (Page<SysDictData>)dataList;
    }

    /**
     * @param sysDictData
     * @return
     */
    private Example createExample(SysDictData sysDictData) {
        Example example = new Example(SysDictData.class);
        Example.Criteria criteria = example.createCriteria();
        if (sysDictData != null) {
            // uid
            if (sysDictData.getUid() != null && !"".equals(sysDictData.getUid())) {
                criteria.andEqualTo("uid", sysDictData.getUid());
            }
            // oid
            if (sysDictData.getOid() !=null && !"".equals(sysDictData.getOid())) {
                criteria.andEqualTo("oid", sysDictData.getOid());
            }
            // dictTypeUid
            if (sysDictData.getDictTypeUid() != null && !"".equals(sysDictData.getDictTypeUid())) {
                criteria.andEqualTo("dictTypeUid", sysDictData.getDictTypeUid());
            }
            //dictLabel
            if (sysDictData.getDictLabel() != null && !"".equals(sysDictData.getDictLabel())) {
                criteria.andEqualTo("dictLabel", sysDictData.getDictLabel());
            }
            //dictValue
            if (sysDictData.getDictValue() != null && !"".equals(sysDictData.getDictValue())) {
                criteria.andEqualTo("dictValue", sysDictData.getDictValue());
            }
            //listClass
            if (sysDictData.getListClass() != null && !"".equals(sysDictData.getListClass())) {
                criteria.andEqualTo("listClass", sysDictData.getListClass());
            }
            //isDefault
            if (sysDictData.getIsDefault() != null  && !"".equals(sysDictData.getIsDefault())) {
                criteria.andEqualTo("isDefault", sysDictData.getIsDefault());
            }
            // 备注
            if (sysDictData.getRemark() != null && !"".equals(sysDictData.getRemark())) {
                criteria.andLike("remark", "%" + sysDictData.getRemark() + "%");
            }
            //状态
            if (sysDictData.getStatus() !=null && !"".equals(sysDictData.getStatus())) {
                criteria.andEqualTo("status", sysDictData.getStatus());
            }
            //是否发布(1:是，0:否)
            if (sysDictData.getIsPublish() != null && !"".equals(sysDictData.getIsPublish())) {
                criteria.andEqualTo("isPublish", sysDictData.getIsPublish());
            }

        }
        return example;
    }

    /**
     * 根据字典名称查询字典数据
     *
     * @param dictType
     * @return
     */
    @Override
    public Map<String, Object> getListByDictType(String dictType) {

        int i=1;
        Map<String, Object> map = new HashMap<>();
        Example example = new Example(SysDictType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dictType", dictType);
        List<SysDictType> sysDictTypes = sysDictTypeMapper.selectByExample(example);
        List<SysDictData> sysDictData=new ArrayList<>();
        for (SysDictType sysDictType : sysDictTypes) {
            Example example1 = new Example(SysDictData.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("dictTypeUid", sysDictType.getUid());
            sysDictData = sysDictDataMapper.selectByExample(example1);
        }
        map.put("list", sysDictData);
        map.put("defaultValue", i);


        return map;
    }

    /**
     * 根据字典名臣数组查询字典数据
     *
     * @param dictNames
     * @return
     */
    @Override
    public Map<String, Map> getListByDictTypeList(String[] dictNames) {
        Map<String, Map> result = new HashMap<>();
        int i=1;
        for (String dictName : dictNames) {
            Map<String, Object> map = new HashMap<>();
            SysDictType sysDictType=new SysDictType();
            sysDictType.setDictType(dictName);
            SysDictType sysDictType1 = sysDictTypeMapper.selectOne(sysDictType);

            Example example = new Example(SysDictData.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("dictTypeUid", sysDictType1.getUid());
            List<SysDictData> sysDictDataList = sysDictDataMapper.selectByExample(example);
            map.put("list",sysDictDataList );
            map.put("defaultValue", i);
            i++;
            result.put(dictName, map);
        }

        return result;
    }

    /**
     * 根据字典类型获取字典数据
     *
     * @Param dictType
     */
    @Override
    public Map<String, Object> getMapByDictType(String dictType) {

        Map<String, Object> map = new HashMap<>();

        Example example = new Example(SysDictType.class);
        example.createCriteria().andEqualTo("dictType", dictType);
        SysDictType sysDictType = sysDictTypeMapper.selectOneByExample(example);

        Example example2 = new Example(SysDictData.class);
        example2.createCriteria().andEqualTo("dictTypeUid", sysDictType.getUid());
        List<SysDictData> sysDictDatas = sysDictDataMapper.selectByExample(example2);
        if (sysDictDatas != null && sysDictDatas.size() > 0) {
            for (SysDictData sysDictData : sysDictDatas) {
                if (sysDictData.getIsDefault() == 1) {
                    map.put("defaultValue", sysDictData.getDictValue());
                }
            }
            map.put("list", sysDictDatas);
        }


        return map;
    }


}
